<?php
namespace Admin\Model;

use Common\Model\CommonModel;

class CardRechargeModel extends CommonModel {

    public function get_list($where = array(),$order = '',$from =  0,$to = 0){
        $list = M("Recharge")->alias("r")->field("r.*,l.logistics_name,u.user_nicename,u.user_phone")->where($where)
                ->join("bdt_logistics l ON l.id = r.rid")
                ->join("bdt_users u ON u.id = r.uid","LEFT")
                ->order($order)
                ->limit($from , $to)
                ->select();

        return $list;
    }

    public function get_card_list($where = array(),$order = '',$from =  0,$to = 0){
        $where['r.model'] = 2;
        $list = M("Recharge")->alias("r")->field("r.*,l.logistics_name,c.number AS card_number,c.vip_name,c.vip_phone,c.vip_plate_number,u.user_nicename,u.user_phone,s.sale_name")->where($where)
            ->join("bdt_card c ON c.id = r.rid","inner")
            ->join("bdt_logistics l ON l.id = c.lid","LEFT")
            ->join("bdt_sale s ON s.id = r.sale_id","LEFT")
            ->join("bdt_users u ON u.id = r.uid","LEFT")
            ->order($order)
            ->limit($from , $to)
            ->select();
        return $list;
    }

    public function get_sale_manager_list($where = array(),$order = '',$from =  0,$to = 0){
        $list = M("Recharge")->alias("r")->field("r.*,m.sale_manage_name,u.user_nicename,u.user_phone")->where($where)
            ->join("bdt_sale_manager m ON m.id = r.rid","LEFT")
            ->join("bdt_users u ON u.id = r.uid","LEFT")
            ->order($order)
            ->limit($from , $to)
            ->select();
        return $list;
    }

    /*老版本-用户数据来源不同*/
    // public function get_list($where = array(),$order = '',$from =  0,$to = 0){
    //     $list = M("Recharge")->alias("r")->field("r.*,l.logistics_name,u.user_nicename,u.user_phone")->where($where)
    //             ->join("bdt_logistics l ON l.id = r.rid")
    //             ->join("bdt_users u ON u.id = r.uid","LEFT")
    //             ->order($order)
    //             ->limit($from , $to)
    //             ->select();

    //     return $list;
    // }

    // public function get_card_list($where = array(),$order = '',$from =  0,$to = 0){
    //     $where['r.model'] = 2;
    //     $list = M("Recharge")->alias("r")->field("r.*,l.logistics_name,c.number AS card_number,car.car_code,u.user_nicename,u.user_phone")->where($where)
    //         ->join("bdt_card c ON c.id = r.rid","inner")
    //         ->join("bdt_logistics l ON l.id = c.lid","LEFT")
    //         ->join("bdt_car car ON car.card_id = c.id","LEFT")
    //         ->join("bdt_users u ON u.id = r.uid","LEFT")
    //         ->order($order)
    //         ->limit($from , $to)
    //         ->select();
    //     return $list;
    // }

    // public function get_sale_manager_list($where = array(),$order = '',$from =  0,$to = 0){
    //     $list = M("Recharge")->alias("r")->field("r.*,m.sale_manage_name,u.user_nicename,u.user_phone")->where($where)
    //         ->join("bdt_sale_manager m ON m.id = r.rid","LEFT")
    //         ->join("bdt_users u ON u.id = r.uid","LEFT")
    //         ->order($order)
    //         ->limit($from , $to)
    //         ->select();
    //     return $list;
    // }
    /*老版本-用户数据来源不同*/

    /**
     * 新增充值记录
     * $model = 1 物流公司充值  =2  会员充值 =3上级销售机构充值
     * op = 2 为审核成功  3为审核失败
     */
    public function insert_data($insert,$model,$op = "", $lid = null,$uid = null){
        if(empty($op)){
            $rid = $insert['rid'];  //物流公司 或 会员ID
            if(empty($rid)){
                return -1;
            }
            $insert['price'] = round($insert['price'],2);
            $insert['general_price'] = round($insert['general_price'],2);
            $insert['self_price'] =  round($insert['self_price'],2);
            $insert['active_price'] =  round($insert['active_price'],2);
            $insert['create_time'] = time();
            $insert['model'] = $model;
            $insert['uid'] = $uid?$uid:session('ADMIN_ID');
            $insert['number'] = date("YmdHis").rand(10000,99999);

            $price = round($insert['price']+$insert['general_price']+$insert['self_price']-$insert["active_price"],2);
            if($model == 2){   //会员充值
                $insert['lid'] = $lid?$lid:get_logistics_id();
                $logistics =  M("Logistics")->where(array("id"=>$lid))->find();
                $card =  M("Card")->where(array("id"=>$rid))->find();
                if(empty($card)){
                    return -2;
                }
                $insert["status"] = 2;
                $insert['before_balance'] = $card['balance'];
                $insert['after_balance'] = round($card['balance']+$price,2);
            }

            $result =  M("Recharge")->add($insert);
        }

        if($result && ($insert["status"] == 2 || $op == 2)){    //管理员充值或审核成功进入账单
            if($model == 2){
                $lid = $logistics['id'];
                M("Card")->where(array("id"=>$card['id']))->save(array("balance"=>round($card['balance']+$price,2)));
                D("Logistics/LogisticsBill")->insert_data($lid,$insert['price'],3);
                //激活充值赠送
                if(!empty($insert['active_price'])){
                    //D("Logistics/LogisticsBill")->insert_data($lid,$insert['active_price'],4);
                }
                //卡片充值赠送账单
                $give_price = $insert['self_price'];
                if(!empty($give_price)){
                    D("Logistics/LogisticsBill")->insert_data($lid,$give_price,6);
                }
                return $result;
            }
        }else{
            return -3;
        }
    }

    /**
     * 新增充值记录
     * $model = 1 物流公司充值  =2  会员充值 =3上级销售机构充值
     * op = 2 为审核成功  3为审核失败
     */
    public function pre_insert_data($insert,$model,$op = "", $lid = null,$uid = null){
        $rid = $insert['rid'];  //物流公司 或 会员ID
        if(empty($rid)){
            return -1;
        }

        $admin_card_give_price = get_config_by_key('admin_card_give_price');    //管理员充值赠送金额
        $logistics_card_give_price = get_config_by_key('logistics_card_give_price');    //管理员开发费

        if ($insert['sale_id']) {
            $sale = M("Sale")->field('s.*, sm.sale_manage_name, sm.balance as upper_balance, sm.give_percent')->alias('s')->join("bdt_sale_manager sm ON sm.id = s.sale_manager_id","LEFT")->where(array("s.id"=>$insert['sale_id']))->find();
            $sale_give_percent = $sale['give_percent'];
            $insert['self_price'] = $insert['price'] * $sale_give_percent / 100;
            $insert['general_price'] = $admin_card_give_price;
        }else{
            $insert['general_price'] = $admin_card_give_price;
            $insert['self_price'] = $logistics_card_give_price;
        }

        $insert['price'] = round($insert['price'],2);
        $insert['general_price'] = round($insert['general_price'],2);
        $insert['self_price'] =  round($insert['self_price'],2);
        $insert['active_price'] =  round($insert['active_price'],2);
        $insert['create_time'] = time();
        $insert['model'] = $model;
        $insert['uid'] = $uid?$uid:session('ADMIN_ID');
        $insert['number'] = date("YmdHis").rand(10000,99999);

        $insert['lid'] = $lid?$lid:get_logistics_id();
        $result =  M("Recharge")->add($insert);

        if($result){
            return $insert;
        }else{
            return -3;
        }
    }

    public function update_status($recharge_number){
        $recharge = $this->where(array('number'=>$recharge_number))->find();

        if (!empty($recharge) && $recharge['status'] == 1) {

            $rid = $recharge['rid'];  //物流公司 或 会员ID
            if(empty($rid)){
                return -1;
            }

            $price = round($recharge['price']+$recharge['general_price']+$recharge['self_price']-$recharge["active_price"],2);
            
            $lid = $recharge['lid'];

            $logistics =  M("Logistics")->where(array("id"=>$lid))->find();
            $card =  M("Card")->where(array("id"=>$rid))->find();
            if(empty($card)){
                return -2;
            }
            $recharge_update["status"] = 2;
            $recharge_update['before_balance'] = $card['balance'];
            $recharge_update['after_balance'] = round($card['balance']+$price,2);

            $result =  M("Recharge")->where(array('number'=>$recharge_number))->save($recharge_update);
            if($result){    //管理员充值或审核成功进入账单
                
                M("Card")->where(array("id"=>$card['id']))->save(array("balance"=>round($card['balance']+$price,2)));
                D("Logistics/LogisticsBill")->insert_data($lid,$recharge['price'],3);
                //激活充值赠送
                if(!empty($recharge['active_price'])){
                    //D("Logistics/LogisticsBill")->insert_data($lid,$recharge['active_price'],4);
                }
                //卡片充值赠送账单
                $give_price = $recharge['self_price'];
                if(!empty($give_price)){
                    D("Logistics/LogisticsBill")->insert_data($lid,$give_price,6);
                }

                $card_update =array("recharge_num"=>$card['recharge_num']+1);
                if($card['recharge_num'] == 0){
                    $card_update['is_active'] = 1;
                    $card_update['active_price'] = $recharge['active_price'];
                    M("Logistics")->where(array("id"=>$logistics['id']))->save(array('active_card_num'=>$logistics['active_card_num']+1));
                }
                $this->where(array("id"=>$card['id']))->save($card_update);

                return $recharge;
            }else{
                return -3;
            }
        }
    }
}